package com.skyz.base.activity;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.luck.picture.lib.utils.ToastUtils;
import com.skyz.base.R;
import com.skyz.base.util.EventBusUtils;
import com.skyz.base.util.ScreenUtils;
import com.skyz.base.util.ViewUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;


public abstract class BaseActivity extends AppCompatActivity{

    protected final String TAG = getClass().getSimpleName();

    //当前activity对象实例
    protected BaseActivity mActivity;

    //当前activity状态栏字体颜色是否为黑色
    private boolean mStatusBarDarkText = true;
    //当前activity是否全屏展示（是否将布局延申至状态栏）
    private boolean mLayoutFullScreen = false;
    //当前activity状态栏背景颜色
    private int mStatusBarBackgroundColor = Color.TRANSPARENT;
    //当前activity内容背景颜色
    private int mContentBackgroundColor = Color.WHITE;

    private View mViewBaseStatusBar;
    private FrameLayout mFlBaseContent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = this;
//        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        EventBusUtils.register(this);

        // 此处的思路是：
        // 1.将系统的状态栏隐藏并将布局铺满全屏，
        // 2.在布局中放置一个view用来代替系统状态栏。
        // 以达到方便实现动态修改状态栏高度和颜色的需求。
        hideSystemStatusBar();

        //初始化状态栏字体颜色
        initStatusBarDarkText();

        //加载base根布局
        setContentView(R.layout.activity_base);

        //初始化base根布局控件
        initBaseView();

        //初始化全屏展示
        initLayoutFullScreen();

        //初始化状态栏背景颜色
        initStatusBarBackgroundColor();

        //初始化base内容视图
        initBaseContentView();

        //初始化base内容视图背景颜色
        initContentBackgroundColor();

        onLayoutInflated();
        Bundle  savedInstance = savedInstanceState==null?getIntent().getExtras():savedInstanceState;
        initData(savedInstance);
    }

    @Override
    protected void onDestroy() {
        if (EventBusUtils.isRegistered(mActivity)) {
            EventBusUtils.unregister(mActivity);
        }
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleEvent(Object event) {
        onMessageEvent(event);
    }

    protected abstract void onMessageEvent(Object event);

    private void hideSystemStatusBar() {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.setStatusBarColor(Color.TRANSPARENT);
        //将布局延申到状态栏
        int systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
        window.getDecorView().setSystemUiVisibility(systemUiVisibility);
    }

    private void initStatusBarDarkText() {
        //状态栏字体颜色仅支持Android 6.0以上设备
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Window window = getWindow();
            //将布局延申到状态栏
            int systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            if (mStatusBarDarkText) {
                systemUiVisibility = systemUiVisibility | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
            }
            window.getDecorView().setSystemUiVisibility(systemUiVisibility);
        }
    }

    /**
     * 根据UI设计稿修改状态栏字体颜色{@link #mStatusBarDarkText}.
     *
     * @param darkText true:黑色 false:白色
     */
    protected void setStatusBarDarkText(boolean darkText) {
        mStatusBarDarkText = darkText;
        initStatusBarDarkText();
    }

    /**
     * 根据UI设计稿是否全屏显示{@link #mLayoutFullScreen}.
     *
     * @param fullScreen true:全屏 false:非全屏
     */
    protected void setLayoutFullScreen(boolean fullScreen) {
        mLayoutFullScreen = fullScreen;
        initLayoutFullScreen();
    }

    /**
     * 根据UI设计稿修改状态栏颜色{@link #mStatusBarBackgroundColor}.
     *
     * @param color 颜色
     */
    protected void setStatusBarBackgroundColor(int color) {
        mStatusBarBackgroundColor = color;
        initStatusBarBackgroundColor();
    }


    protected void setContentBackgroundColor(int color) {
        mContentBackgroundColor = color;
        initContentBackgroundColor();
    }

    private void initBaseView() {
        mViewBaseStatusBar = findViewById(R.id.view_base_status_bar);
        mFlBaseContent = findViewById(R.id.fl_base_content);
    }

    private void initLayoutFullScreen() {
        if (mLayoutFullScreen) {
            ViewUtils.setHeight(mViewBaseStatusBar, 0);
        } else {
            ViewUtils.setHeight(mViewBaseStatusBar, ScreenUtils.getStatusBarHeight());
        }
    }

    private void initStatusBarBackgroundColor() {
        mViewBaseStatusBar.setBackgroundColor(mStatusBarBackgroundColor);
    }

    private void initContentBackgroundColor() {
        if (mFlBaseContent == null) {
            return;
        }
        mFlBaseContent.setBackgroundColor(mContentBackgroundColor);
    }

    private void initBaseContentView() {
        if (mFlBaseContent == null) {
            return;
        }
        //获取到 子类Activity 的布局id，并将布局加载进来
        int layoutId = getLayoutId();
        if (layoutId == 0) {
            Log.w(TAG, "You may forget to fill in the layout id!");
            return;
        }
        mFlBaseContent.removeAllViews();
        LayoutInflater.from(mActivity).inflate(layoutId, mFlBaseContent, true);
    }

    protected abstract int getLayoutId();

    protected abstract void onLayoutInflated();

    protected abstract void initData(Bundle savedInstanceState);


}
